package com.example.light.web.meeting.service;

import ch.qos.logback.core.util.FileUtil;
import com.example.light.common.exception.GlobalException;
import com.example.light.common.result.CodeMsg;
import com.example.light.common.result.Result;
import com.example.light.common.util.UploadFileUtil;
import com.example.light.redis.MeetKey;
import com.example.light.redis.RedisService;
import com.example.light.redis.UserKey;
import com.example.light.web.meeting.dao.OaMeetAttendeeMapper;
import com.example.light.web.meeting.dao.OaMeetRecordMapper;
import com.example.light.web.meeting.entity.OaMeetAttendee;
import com.example.light.web.meeting.entity.OaMeetRecord;
import com.example.light.web.meeting.vo.MeetRecordVO;
import com.example.light.web.system.entity.User;
import com.fasterxml.jackson.databind.util.ClassUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ClassUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lizhilong
 * @Description:
 * @Date: Created in 21:06 2018/8/27
 * @Modified By:
 */
@Service("MeetServiceImpl")
public class MeetServiceImpl implements MeetService{

    @Autowired
    private OaMeetRecordMapper meetRecordMapper;
    @Autowired
    private OaMeetAttendeeMapper meetAttendeeMapper;
    @Autowired
    private RedisService redisService;
    @Override
    public PageInfo listMeetRecord(Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        List<OaMeetRecord> meetList = meetRecordMapper.selectMeetList();
        return new PageInfo(meetList);
    }

    @Override
    public PageInfo listMeetRecord(Integer pageNumber, Integer pageSize, String searchInfo) {
        PageHelper.startPage(pageNumber,pageSize);
        List<OaMeetRecord> meetList = meetRecordMapper.selectMeetListByMeetTheme(searchInfo);
        return new PageInfo(meetList);
    }

    @Override
    public void addMeetRecord(OaMeetRecord meetRecord,Integer[] attendee) {
        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMDD");
        DateTime current = new DateTime();
        int meetYear = current.getYear()%100;
        int meetMonth = current.getMonthOfYear();
        int meetDay = current.getDayOfMonth();
        double meetCurrent = meetYear*Math.pow(10,8) + meetMonth*Math.pow(10,6) + meetDay*Math.pow(10,4);
        int meetID = meetRecordMapper.selectCurrentLastRecord((int) meetCurrent);
        meetID = meetID == 0 ? (int)meetCurrent+1 : meetID+1;
        meetRecord.setMeetId(meetID);
        meetRecordMapper.insertSelective(meetRecord);
        addMeetAttendee(meetRecord,attendee);
    }

    @Override
    public void deleteMeetRecord(Integer[] meetIds) {
        meetRecordMapper.deleteBatchByMeetId(meetIds);
    }

    @Override
    public void updateMeetRecord(MeetRecordVO meetRecordVO) {
        //如果缓存会议为空抛出异常
        if(meetRecordVO == null) throw new GlobalException(CodeMsg.MeetingCodeMsg.NO_MEETRECORD);
        MeetRecordVO meet =redisService.get(new MeetKey("MeetRecordVO"), "meetRecord",
                MeetRecordVO.class);

        if(meet == null) throw new GlobalException(CodeMsg.MeetingCodeMsg.NO_MEETRECORD);
        meetRecordVO.setMeetId(meet.getMeetId());
        List<OaMeetAttendee> attendeeDO = new ArrayList<>();
        Integer[] attendee = meetRecordVO.getAddAttendeesId();

        meetRecordMapper.updateByPrimaryKeySelective(meetRecordVO);
        if(null != meetRecordVO.getAddAttendeesId() && meetRecordVO.getAddAttendeesId().length != 0){
            for (int i = 0; i < meetRecordVO.getAddAttendeesId().length; i++){
                attendeeDO.add(new OaMeetAttendee(attendee[i],meetRecordVO.getMeetId()));
            }
            meetAttendeeMapper.insertMeetAttendee(attendeeDO);
        }
        if(null != meetRecordVO.getDeleteAttendeesId() && meetRecordVO.getDeleteAttendeesId().length != 0)
        meetAttendeeMapper.deleteMeetAttendee(meetRecordVO.getMeetId(),meetRecordVO.getDeleteAttendeesId());

    }



    @Override
    public void addMeetAttendee(OaMeetRecord meetRecord, Integer[] attendee) {

        List<OaMeetAttendee> attendeeDO = new ArrayList<>();
        for (int i = 0; i < attendee.length; i++){
            attendeeDO.add(new OaMeetAttendee(attendee[i],meetRecord.getMeetId()));
        }
        meetAttendeeMapper.insertMeetAttendee(attendeeDO);
    }

    @Override
    public MeetRecordVO getMeetByMeetId(Integer meetId) {
        MeetRecordVO meetRecordVO = meetRecordMapper.selectMeetRecordByMeetId(meetId);
        return meetRecordVO;
    }

    /**
    *@description: 添加缓存到redis
    */
    @Override
    public void addCookie(HttpServletResponse response, MeetRecordVO meet){
        redisService.set(new MeetKey("MeetRecordVO"), "meetRecord", meet);
        Cookie cookie = new Cookie("meetId", meet.getMeetId().toString());
        cookie.setMaxAge(30*60);
        response.addCookie(cookie);
    }

    @Override
    public void addMeetSummary(String meetSummary) {
        MeetRecordVO meetRecord = new MeetRecordVO();
        meetRecord.setMeetSummary(meetSummary);
        updateMeetRecord(meetRecord);
    }

    @Override
    public String uploadFile(MultipartFile file, String userPhone) {
        String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
        path = path + "static";
        String realName = file.getOriginalFilename();
        String res = File.separator + "attachment" + File.separator + userPhone + File.separator;
        String dbPath = res.replace(File.separator,"/");
        UploadFileUtil.catFile(file,realName,path + res);
        return dbPath+realName;

    }

    @Override
    public void deleteAttends(Integer id) {

    }

}
